package com.teng.smartdrawer;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ListView;

import com.teng.smartdrawer.adapter.BuyAdapter;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.listener.SwpipeListViewOnScrollListener;
import com.teng.smartdrawer.parser.DataConstants;
import com.teng.smartdrawer.parser.DataParser;

import java.util.ArrayList;
import java.util.List;

public class BuyActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    List<Component> mData = new ArrayList<Component>();
    private BuyAdapter adapter;
    private SwipeRefreshLayout refresh_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);

        ActionBar actionBar = getSupportActionBar();
        //显示返回图表
        actionBar.setDisplayHomeAsUpEnabled(true);
        //设置ActionBar左边默认的图标是否可用
        actionBar.setDisplayUseLogoEnabled(true);
        // 显示窗口标题
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("购买");

        refresh_layout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);

        refresh_layout.setColorSchemeResources(R.color.black, R.color.google_blue,
                R.color.google_yellow, R.color.google_blue, R.color.google_yellow);

        refresh_layout.setDistanceToTriggerSync(500);   // 下拉刷新的距离
        refresh_layout.setOnRefreshListener(this);

        refresh_layout.post(new Runnable() {
            @Override
            public void run() {
                refresh_layout.setRefreshing(true);
            }
        });

        ListView listView = (ListView) findViewById(R.id.buy_list);

        listView.setOnScrollListener(new SwpipeListViewOnScrollListener(refresh_layout));

        adapter = new BuyAdapter(mData , this);

        listView.setAdapter(adapter);

        DataParser.getBuyData(this , handler);

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            refresh_layout.setRefreshing(false);

            switch (msg.what){

                case DataConstants.BUY_COMPONENT_SIGN:

                    List<Component> list = (List<Component>) msg.obj;

                    mData.clear();

                    mData.addAll(list);

                    adapter.notifyDataSetChanged();

                    break;

            }

            return false;
        }
    });

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == android.R.id.home)
        {
            // TODO actionBar返回处理
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    public void onRefresh() {
        DataParser.getBuyData(this , handler);
    }
}
